package org.dong.samples.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.dong.samples.dao.TestHibernateDAO;
import org.dong.samples.model.ExamTest;
import org.dong.samples.model.Score;
import org.dong.samples.model.User;
import org.dong.samples.model.vo.ExamTestVO;
import org.dong.samples.model.vo.MessageVO;
import org.dong.samples.model.vo.ScoreVO;
import org.dong.samples.model.vo.SubjectVO;
import org.dong.samples.service.ScoreService;
import org.dong.samples.service.StudentService;
import org.dong.samples.service.TestHibernateService;
import org.dong.samples.util.NowTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value="/student")
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private TestHibernateService testHibernateService;
	
	@Autowired
	private ScoreService scoreService;
	
	@Autowired
	private TestHibernateDAO testHibernateDAO;
	
	@RequestMapping(value="/subjects")
	public String toSubjects(HttpSession session, Model model) throws Exception{
		User user = (User) session.getAttribute("user");
		List<SubjectVO> examSubjectList = studentService.getSubjectList(user);
		model.addAttribute("examSubjectList", examSubjectList);
		return "student/subjects";
	}
	
	@ResponseBody
	@RequestMapping(value="/showScore", method = RequestMethod.POST)
	public ScoreVO showScore(Integer scoreId){
		ScoreVO vo = studentService.getScoreById(scoreId);
		return vo;
	}
	
	@RequestMapping(value="/toTest/{subId}", method = RequestMethod.GET)
	public String toTest(Model model,  @PathVariable Integer subId, HttpSession session) throws Exception{
		User user = (User) session.getAttribute("user");
		List<ExamTest> list = studentService.getTestListBySubject(subId,user.getUserId());
		session.setAttribute("subId", subId);
		model.addAttribute("list", list);
		model.addAttribute("count", list.size());
		return "student/test";
	}
	
	@ResponseBody
	@RequestMapping(value = "/postAnwer", method = RequestMethod.POST)
	public MessageVO assignment(HttpSession session, HttpServletRequest request,@RequestParam(name = "testId[]")String[] testId, @RequestParam(name = "anwer[]")String[] anwer) throws Exception{
		List<String> tId = new ArrayList<String>();
		List<String> anw = new ArrayList<String>();
		User user = (User) session.getAttribute("user");
		int subId = (int) session.getAttribute("subId");
		MessageVO vo = new MessageVO();
		int score = 0;
		for(String id:testId){
			tId.add(id);
		}
		for(String an:anwer){
			anw.add(an);
		}
		for(int i=0;i<testId.length;i++){
			ExamTest test = testHibernateDAO.getTestById(Integer.valueOf(tId.get(i)));
			String a = anw.get(i);
			if(test!=null){
				if(a.equals(test.getAnswer())){
					score+=20;
				}
			}
		}
		Score s = new Score();
		s.setUserId(user.getUserId());
		s.setSubjectId(subId);
		s.setScore(score);
		s.setExamTime(NowTime.getTimeTimestamp());
		scoreService.save(s);;
		vo.setMassage(String.valueOf(score));
		return vo;
	}
}
